import com.jeta.forms.components.separator.TitledSeparator;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;


public class frmNewCustomer extends JPanel
{
   JLabel lblFirstName = new JLabel();
   JTextField txtZip = new JTextField();
   JTextField txtCustomerID = new JTextField();
   JTextField txtAddress = new JTextField();
   JTextField txtCity = new JTextField();
   JTextField txtName = new JTextField();
   JTextField txtState = new JTextField();
   JLabel lblFirstName1 = new JLabel();
   JLabel lblName = new JLabel();
   JLabel lblFirstName2 = new JLabel();
   JLabel lblFirstName3 = new JLabel();
   JTextField txtPin = new JTextField();
   JLabel lblName1 = new JLabel();
   JTextField txtSSN = new JTextField();
   JButton btnSave = new JButton();
   JButton btnCancel = new JButton();
   JTextField txtDistanceProvider = new JTextField();
   JCheckBox chkVoice = new JCheckBox();
   JCheckBox chkCallerID = new JCheckBox();
   JCheckBox chkThreeWay = new JCheckBox();
   Properties displayedCustomer;
   JPanel me = this;

   /**
    * Default constructor
    */
   public frmNewCustomer()
   {
      initializePanel();
   }
   
   frmNewCustomer(JFrame caller)
   {
	   initializePanel();
	   
	   caller.setVisible(false);
	   caller.dispose();
   }

   /**
    * Adds fill components to empty cells in the first row and first column of the grid.
    * This ensures that the grid spacing will be the same as shown in the designer.
    * @param cols an array of column indices in the first row where fill components should be added.
    * @param rows an array of row indices in the first column where fill components should be added.
    */
   void addFillComponents( Container panel, int[] cols, int[] rows )
   {
      Dimension filler = new Dimension(10,10);

      boolean filled_cell_11 = false;
      CellConstraints cc = new CellConstraints();
      if ( cols.length > 0 && rows.length > 0 )
      {
         if ( cols[0] == 1 && rows[0] == 1 )
         {
            /** add a rigid area  */
            panel.add( Box.createRigidArea( filler ), cc.xy(1,1) );
            filled_cell_11 = true;
         }
      }

      for( int index = 0; index < cols.length; index++ )
      {
         if ( cols[index] == 1 && filled_cell_11 )
         {
            continue;
         }
         panel.add( Box.createRigidArea( filler ), cc.xy(cols[index],1) );
      }

      for( int index = 0; index < rows.length; index++ )
      {
         if ( rows[index] == 1 && filled_cell_11 )
         {
            continue;
         }
         panel.add( Box.createRigidArea( filler ), cc.xy(1,rows[index]) );
      }

   }

   /**
    * Helper method to load an image file from the CLASSPATH
    * @param imageName the package and name of the file to load relative to the CLASSPATH
    * @return an ImageIcon instance with the specified image file
    * @throws IllegalArgumentException if the image resource cannot be loaded.
    */
   public ImageIcon loadImage( String imageName )
   {
      try
      {
         ClassLoader classloader = getClass().getClassLoader();
         java.net.URL url = classloader.getResource( imageName );
         if ( url != null )
         {
            ImageIcon icon = new ImageIcon( url );
            return icon;
         }
      }
      catch( Exception e )
      {
         e.printStackTrace();
      }
      throw new IllegalArgumentException( "Unable to load image: " + imageName );
   }

   public JPanel createPanel()
   {
      JPanel jpanel1 = new JPanel();
      FormLayout formlayout1 = new FormLayout("FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:80PX:NONE,FILL:41PX:NONE,FILL:47PX:NONE,FILL:17PX:NONE,FILL:45PX:NONE,FILL:45PX:NONE,FILL:17PX:NONE,FILL:53PX:NONE,FILL:DEFAULT:NONE,FILL:71PX:NONE,FILL:15PX:NONE","CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE");
      CellConstraints cc = new CellConstraints();
      jpanel1.setLayout(formlayout1);

      lblFirstName.setName("lblFirstName");
      lblFirstName.setText("Zip:");
      jpanel1.add(lblFirstName,cc.xy(10,13));

      txtZip.setName("txtZip");
      jpanel1.add(txtZip,cc.xywh(11,13,2,1));

      txtCustomerID.setName("txtCustomerID");
      txtCustomerID.setVisible(false);
      jpanel1.add(txtCustomerID,cc.xywh(3,4,3,1));

      txtAddress.setName("txtAddress");
      jpanel1.add(txtAddress,cc.xywh(3,12,10,1));

      txtCity.setName("txtCity");
      jpanel1.add(txtCity,cc.xywh(3,13,3,1));

      txtName.setName("txtName");
      jpanel1.add(txtName,cc.xywh(3,10,10,1));

      txtState.setName("txtState");
      jpanel1.add(txtState,cc.xy(8,13));

      lblFirstName1.setName("lblFirstName");
      lblFirstName1.setText("State:");
      jpanel1.add(lblFirstName1,cc.xy(7,13));

      JLabel jlabel1 = new JLabel();
      jlabel1.setText("Customer ID:");
      jlabel1.setVisible(false);
      jpanel1.add(jlabel1,cc.xy(2,4));

      lblName.setName("lblName");
      lblName.setText("Name:");
      jpanel1.add(lblName,cc.xy(2,10));

      lblFirstName2.setName("lblFirstName");
      lblFirstName2.setText("Street:");
      jpanel1.add(lblFirstName2,cc.xy(2,12));

      lblFirstName3.setName("lblFirstName");
      lblFirstName3.setText("City:");
      jpanel1.add(lblFirstName3,cc.xy(2,13));

      TitledSeparator titledseparator1 = new TitledSeparator();
      titledseparator1.setText("Personal Information");
      jpanel1.add(titledseparator1,cc.xywh(2,8,11,1));

      JLabel jlabel2 = new JLabel();
      jlabel2.setText("Pin");
      jpanel1.add(jlabel2,cc.xy(2,6));

      txtPin.setName("txtPin");
      jpanel1.add(txtPin,cc.xy(3,6));

      lblName1.setName("lblName");
      lblName1.setText("SSN:");
      jpanel1.add(lblName1,cc.xy(2,11));

      txtSSN.setName("txtSSN");
      jpanel1.add(txtSSN,cc.xywh(3,11,3,1));

      JLabel jlabel3 = new JLabel();
      jlabel3.setFont(new Font("Tahoma",Font.BOLD,18));
      jlabel3.setText("Create New Customer");
      jpanel1.add(jlabel3,cc.xywh(2,2,11,1));

      btnSave.setActionCommand("Save");
      btnSave.setName("btnSave");
      btnSave.setText("Save");
      jpanel1.add(btnSave,cc.xywh(2,22,2,1));

      btnCancel.setActionCommand("Cancel");
      btnCancel.setName("btnCancel");
      btnCancel.setText("Cancel");
      jpanel1.add(btnCancel,cc.xywh(5,22,4,1));

      TitledSeparator titledseparator2 = new TitledSeparator();
      titledseparator2.setText("Personal Information");
      jpanel1.add(titledseparator2,cc.xywh(2,15,11,1));

      JLabel jlabel4 = new JLabel();
      jlabel4.setText("Distance Provider:");
      jpanel1.add(jlabel4,cc.xy(2,17));

      txtDistanceProvider.setName("txtDistanceProvider");
      jpanel1.add(txtDistanceProvider,cc.xywh(3,17,3,1));

      chkVoice.setActionCommand("Voice");
      chkVoice.setName("chkVoice");
      chkVoice.setText("Voice");
      jpanel1.add(chkVoice,cc.xywh(7,17,6,1));

      chkCallerID.setActionCommand("Voice");
      chkCallerID.setName("chkCallerID");
      chkCallerID.setText("Caller ID");
      jpanel1.add(chkCallerID,cc.xywh(7,18,6,1));

      chkThreeWay.setActionCommand("Voice");
      chkThreeWay.setName("chkThreeWay");
      chkThreeWay.setText("Three-Way");
      jpanel1.add(chkThreeWay,cc.xywh(7,19,6,1));

      addFillComponents(jpanel1,new int[]{ 1,2,3,4,5,6,7,8,9,10,11,12,13 },new int[]{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23 });
      
	  btnSave.addActionListener( new ActionListener()
	  {
	    	public void actionPerformed( ActionEvent evt )
	    	{
	    		loadDisplayWithForm();
	    		displayedCustomer = ClientHelper.CreateCustomer(displayedCustomer, me);
	    		ClientHelper.CustomerLogout(displayedCustomer.getProperty(Constants.ACCOUNT_NUMBER));
	    		frmManageCustomer form = new frmManageCustomer(displayedCustomer.getProperty(Constants.ACCOUNT_NUMBER),
	    														  displayedCustomer.getProperty(Constants.PIN), 
	    														  (JFrame)me.getParent().getParent().getParent().getParent());
	    		JScrollPane scrollable = new JScrollPane(form);
	    		JFrame newFrame = new JFrame();
	            newFrame.add(scrollable);
	            //newFrame.pack();
	            newFrame.setSize(650, 677);
	            newFrame.setLocationRelativeTo(null);
	    		newFrame.setVisible(true);
	    	}
	   });
	  
      btnCancel.addActionListener( new ActionListener()
	    {
	    	public void actionPerformed( ActionEvent evt )
	    	{
	    		    frmManageCustomer form = new frmManageCustomer((JFrame)me.getParent().getParent().getParent().getParent());
		    		JScrollPane scrollable = new JScrollPane(form);
		    		JFrame newFrame = new JFrame();
		            newFrame.add(scrollable);
		            //newFrame.pack();
		            newFrame.setSize(650, 677);
		            newFrame.setLocationRelativeTo(null);
		    		newFrame.setVisible(true);
	    	}
	    });
	  
      return jpanel1;
   }

   /**
    * Initializer
    */
   protected void initializePanel()
   {
      setLayout(new BorderLayout());
      add(createPanel(), BorderLayout.CENTER);
   }

   private void loadDisplayWithForm()
   {
	   displayedCustomer = ClientHelper.GetNewDisplay();
	   displayedCustomer.setProperty(Constants.PIN, txtPin.getText());
	   displayedCustomer.setProperty(Constants.NAME, txtName.getText());
	   displayedCustomer.setProperty(Constants.SSN, txtSSN.getText());
	   displayedCustomer.setProperty(Constants.STREET, txtAddress.getText());
	   displayedCustomer.setProperty(Constants.CITY, txtCity.getText());
	   displayedCustomer.setProperty(Constants.STATE, txtState.getText());
	   displayedCustomer.setProperty(Constants.ZIP, txtZip.getText());
	   
	   if(chkCallerID.isSelected()){
		   displayedCustomer.setProperty(Constants.CALLER_ID, "Y");
	   }
	   else
	   {
		   displayedCustomer.setProperty(Constants.CALLER_ID, "N");
	   }
	   
	   if(chkVoice.isSelected()){
		   displayedCustomer.setProperty(Constants.VOICEMAIL, "Y");
	   }
	   else
	   {
		   displayedCustomer.setProperty(Constants.VOICEMAIL, "N");
	   }
	   
	   if(chkThreeWay.isSelected()){
		   displayedCustomer.setProperty(Constants.THREE_WAY, "Y");
	   }
	   else
	   {
		   displayedCustomer.setProperty(Constants.THREE_WAY, "N");
	   }
	   
	   displayedCustomer.setProperty(Constants.DISTANCE_PROVIDER, txtDistanceProvider.getText());
   }
}
